package com.css.fxfzxczh.modules.lossAssessment.resetPrice.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.entity.Lifeline;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.vo.LifelineVO;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.entity.ResetPrice;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.service.ResetPriceService;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/22
 * @Description: 设定重置单价
 */
@Api(tags = "设定重置单价")
@Slf4j
@RestController
@RequestMapping("/resetPrice")
public class ResetPriceController {

    @Resource
    private ResetPriceService resetPriceService;

    /**
     * 分页列表
     *
     * @param
     * @return
     */
    @GetMapping("/getPage/{xsemId}")
    public RestResponse getPage(HttpServletRequest request, @PathVariable("xsemId") String xsemId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = resetPriceService.getPage(curPage, pageSize,xsemId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 导入
     * @param xsemId
     * @return
     */
    @PostMapping("/reportImport")
    public RestResponse reportImport(HttpServletRequest req, String xsemId){
        RestResponse response = null;
        try {
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = resetPriceService.reportImport(inputStream,xsemId);
            response = RestResponse.succeed(str);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 删除单数据
     *
     * @param Id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Operation(summary = "删除数据")
    public RestResponse deleteById(@PathVariable("id") List<String> Id) {
        try {
            resetPriceService.deleteById(Id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除数据失败!", e);
            return RestResponse.fail("删除数据失败!");
        }
    }

    /**
     * 修改单条数据
     *
     * @param param
     * @return
     */
    @PostMapping("/updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody ResetPrice param) {
        try {
            resetPriceService.updateById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            ResetPrice data = resetPriceService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

}
